package kg.aibek.projecteuler.p10;

import java.util.ArrayList;
import java.util.List;

public class Problem41 {
	public void solve() {
		List<Integer> nList = new ArrayList<Integer>();
		for (int i = 1; i < 8; i++) {
			nList.add(i);
		}
		List<String> sList = getPermutation(nList);

		int prime = 1;
		for (String str : sList) {
			int nPrime = new Integer(str);
			if (isPrime(nPrime)) {
				if (prime < nPrime)
					prime = nPrime;
			}
		}
		System.out.println(prime);
	}

	public static void main(String[] args) {
		Problem41 p = new Problem41();
		p.solve();
	}

	private boolean isPrime(int number) {
		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}

	private List<String> getPermutation(List<Integer> nList) {
		List<String> ssList = new ArrayList<String>();
		if (nList.size() == 0) {
			ssList.add("");
			return ssList;
		}

		for (Integer i : nList) {
			String s = i + "";
			List<Integer> nnList = new ArrayList<Integer>();
			nnList.addAll(nList);
			nnList.remove(i);
			List<String> sList = getPermutation(nnList);
			for (String str : sList) {
				ssList.add(s + str);
			}
		}

		return ssList;
	}
}
